﻿using System;
using System.Collections.Generic;
using Autodesk.Revit.DB;
using YJKArchUtils.Utils;

namespace YJKRevitDimensionText.OneKeyPlanDim.Utility
{
	public class SingleLevelDimDataInfo
	{
		public SingleLevelDimDataInfo(AnnotationSymbol annotationSymbol)
		{
			this.m_AnnotationSymbol = annotationSymbol;
		}

		public bool bStepDownDim
		{
			get
			{
				return this.m_bStepDownDim;
			}
			set
			{
				this.m_bStepDownDim = value;
			}
		}

		public XYZ PtLocation
		{
			get
			{
				return this.m_ptLocation;
			}
			set
			{
				this.m_ptLocation = value;
			}
		}

		public double LevelVal
		{
			get
			{
				return this.m_dLevelVal;
			}
			set
			{
				this.m_dLevelVal = value;
			}
		}

		public double LadderLineLength
		{
			get
			{
				return this.m_dLadderLineLength;
			}
			set
			{
				this.m_dLadderLineLength = value;
			}
		}

		public XYZ DimDir
		{
			get
			{
				return this.m_DimDir.Normalize();
			}
			set
			{
				this.m_DimDir = value;
			}
		}

		public AnnotationSymbol AnnotationSymbol
		{
			get
			{
				return this.m_AnnotationSymbol;
			}
			set
			{
				this.m_AnnotationSymbol = value;
			}
		}

		public bool IsNeedMerge(SingleLevelDimDataInfo singleLevelDimDataInfo)
		{
			if (!this.bStepDownDim || !singleLevelDimDataInfo.bStepDownDim)
			{
				return false;
			}
			XYZ xyz = (this.PtLocation - singleLevelDimDataInfo.PtLocation).Normalize();
			return (xyz.IsAlmostEqualTo(this.DimDir) || xyz.IsAlmostEqualTo(-this.DimDir)) && (xyz.IsAlmostEqualTo(singleLevelDimDataInfo.DimDir) || xyz.IsAlmostEqualTo(-singleLevelDimDataInfo.DimDir)) && !Geometry.LessThan(Common.MMToFeet(20000.0), this.PtLocation.DistanceTo(singleLevelDimDataInfo.PtLocation));
		}

		public bool MergeSingleLevelDimDataInfo(Autodesk.Revit.DB.Document doc, SingleLevelDimDataInfo singleLevelDimDataInfo)
		{
			if (!this.IsNeedMerge(singleLevelDimDataInfo))
			{
				return false;
			}
			double num = this.PtLocation.DistanceTo(singleLevelDimDataInfo.PtLocation);
			this.m_dLadderLineLength = num + this.LadderLineLength / 2.0 + singleLevelDimDataInfo.LadderLineLength / 2.0;
			XYZ xyz = (this.PtLocation + singleLevelDimDataInfo.PtLocation) / 2.0;
			this.m_AnnotationSymbol = (CommonInterface.MoveElement(this.m_AnnotationSymbol, this.m_ptLocation, xyz, doc) as AnnotationSymbol);
			this.m_ptLocation = xyz;
			return true;
		}

		public bool IsRepeatabilityJudg(SingleLevelDimDataInfo singleLevelDimDataInfo, double dLength)
		{
			return Geometry.IsEqual(singleLevelDimDataInfo.m_dLevelVal, this.m_dLevelVal) && Geometry.LessThan(singleLevelDimDataInfo.PtLocation.DistanceTo(this.PtLocation), dLength);
		}

		public void DelLevelDim(Autodesk.Revit.DB.Document doc)
		{
			Transaction transaction = new Transaction(doc, "DelElement");
			try
			{
				transaction.Start();
				if (this.m_AnnotationSymbol != null && !(ElementId.InvalidElementId == this.m_AnnotationSymbol.Id))
				{
					doc.Delete(this.m_AnnotationSymbol.Id);
					transaction.Commit();
				}
			}
			catch
			{
				transaction.RollBack();
			}
		}

		public List<ElementId> m_LstElementId = new List<ElementId>();

		private double m_dLevelVal;

		private double m_dLadderLineLength;

		private XYZ m_DimDir = XYZ.Zero;

		private XYZ m_ptLocation = XYZ.Zero;

		private AnnotationSymbol m_AnnotationSymbol;

		private bool m_bStepDownDim;
	}
}
